package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkFile;
import com.ysd.lis.entity.WkFolder;
import com.ysd.lis.mapper.WkFileMapper;
import com.ysd.lis.mapper.WkFolderMapper;
import com.ysd.lis.request.WkFolderParam;
import com.ysd.lis.service.WkFolderService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-03-04
 */
@Service
@Slf4j
public class WkFolderServiceImpl extends ServiceImpl<WkFolderMapper, WkFolder> implements WkFolderService {
    @Autowired
    WkFolderMapper wkFolderMapper;
    @Autowired
    WkFileMapper wkFileMapper;

    @Override
    public Result findFolderList(WkFolderParam param) {
        LambdaQueryWrapper<WkFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WkFolder::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            queryWrapper.and(wrapper -> wrapper.like(WkFolder::getName, param.getSeachValue())
                    .or().like(WkFolder::getCode, param.getSeachValue()));
        }
        queryWrapper.orderByAsc(WkFolder::getSeq);
        List<WkFolder> folderList = wkFolderMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", folderList);

    }

    @Override
    public Result findFolderTreeByFolderCode(WkFolderParam param) {
        List<WkFolder> returnList = new ArrayList<>();
        LambdaQueryWrapper<WkFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WkFolder::getDelFlag, 0);
        queryWrapper.orderByAsc(WkFolder::getSeq);
        List<WkFolder> folderList = wkFolderMapper.selectList(queryWrapper);
        //查出顶端数据
        Optional<WkFolder> first = folderList.stream().filter(a -> a.getCode().equals(param.getCode())).findFirst();
        if(first.isPresent()){
            WkFolder wkFolder = first.get();
            returnList.add(wkFolder);
            getChild(wkFolder,folderList,returnList);
        }
        return Result.succ(1, "查询成功", returnList);

    }

    private void getChild(WkFolder m, List<WkFolder> folderList,List<WkFolder> returnList) {
        List<WkFolder> result = new ArrayList();
        for (WkFolder to : folderList) {
            if (ToolsUtils.isNotEmpty(to.getPid())&&to.getPid().equals(m.getId())) {
                result.add(to);
            }
        }

        for (WkFolder ma : result) {
            returnList.add(ma);
            getChild(ma, folderList,returnList);
        }

    }



    @Override
    public Result findFolderTreeByCode(WkFolderParam param) {
        LambdaQueryWrapper<WkFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WkFolder::getDelFlag,0);
        queryWrapper.eq(WkFolder::getCode,param.getCode());
        List<WkFolder> pFolder = wkFolderMapper.selectList(queryWrapper);
        List<WkFolder> list=new ArrayList<>();
        if (CollectionUtils.isNotEmpty(pFolder)){
            WkFolder wkFolder = pFolder.get(0);
            list.add(wkFolder);
            LambdaQueryWrapper<WkFolder> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(WkFolder::getDelFlag,0);
            queryWrapper1.eq(WkFolder::getPid,wkFolder.getId());
            List<WkFolder> folders = wkFolderMapper.selectList(queryWrapper1);
            list.addAll(folders);
        }

        return Result.succ(1, "查询成功", list);
    }

    @Override
    public Result findFolderListByCode(WkFolderParam param) {
        if (StringUtils.isBlank(param.getCode())){
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        LambdaQueryWrapper<WkFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WkFolder::getDelFlag,0);
        queryWrapper.eq(WkFolder::getCode,param.getCode());

        queryWrapper.orderByAsc(WkFolder::getSeq);
        List<WkFolder> folderList = wkFolderMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", folderList);

    }

    @Override
    public Result addFolder(WkFolder folder) {
        int insert = wkFolderMapper.insert(folder);
        if (insert > 0) {
            return Result.succ(1, "添加成功", folder);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editFolder(WkFolder folder) {
        int updateById = wkFolderMapper.updateById(folder);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", folder);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result deleteFolder(WkFolder folder) {
        //查询本表中的chilern
        LambdaQueryWrapper<WkFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WkFolder::getPid, folder.getId()).eq(WkFolder::getDelFlag, 0);
        long integer = wkFolderMapper.selectCount(queryWrapper);
        if (integer > 0) {
            return Result.fail(400, "该文件夹下有子文件夹，不能删除", "");
        }
        //查询文件夹中是否有文件
        LambdaQueryWrapper<WkFile> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(WkFile::getFolderId, folder.getId()).eq(WkFile::getDelFlag, 0);
        long cnt = wkFileMapper.selectCount(queryWrapper1);
        if (cnt > 0) {
            return Result.fail(400, "该文件夹下有子文件，不能删除", "");
        }
        int deleteById = wkFolderMapper.deleteById(folder.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }


}
